#include "AnaDef.h"
#include <TTreeStream.h>
#include "XGLUtils.h"

Double_t Mproton   (){ return  0.938272;}
Double_t Mpion     (){ return  0.139570;}
Double_t Mkaon     (){ return  0.493677;}
Double_t Melectron (){ return  5.109989e-04;}

 /*
 //in the tender it is the status bits of the tracks 14 - 16 for electrons, pions and protons
    Int_t tmpbit = 0;
    if(esdtrack->TestBit(BIT(14))) tmpbit+=1;
    if(esdtrack->TestBit(BIT(15))) tmpbit+=2;
    if(esdtrack->TestBit(BIT(16))) tmpbit+=4;
  pidV0 = tmpbit;

   */

/*
(*fStream)<<"tree"<<
      "run="<<run<<
      "mag="<<mag<<
      "charge="<<charge<<
      "pbcen="<<pbcen<<

      "p0="<<p0<<
      "eta="<<eta<<

      "pTPC="<<pTPC<<
      "tpcsig="<<tpcsig<<
      "tpcncls="<<tpcncls<<

      "pTRD="<<pTRD<<
      "trdsig="<<trdsig<<
      "trdnch="<<trdnch<<
      "trdncls="<<trdncls<<

      "kinV0="<<kinV0<<
      "pidV0="<<pidV0;

    for(Int_t ipid=0; ipid<5; ipid++){
      (*fStream)<<"tree"<<
        Form("pidITS%d=",ipid)<<pidITS[ipid]<<
        Form("pidTPC%d=",ipid)<<pidTPC[ipid]<<
        Form("pidTOF%d=",ipid)<<pidTOF[ipid];
    }
      
    (*fStream)<<"tree"<<
      "\n";
*/

void convertBG(TString fin)
{
  TFile *fmpv = new TFile("bgmean.root");
  TH1D *hfit = 0x0;

  if(!fmpv->IsOpen()){
    printf("\n\n ******************* no fmpv reference!! **********\n\n\n");
  }
  else{
    hfit=(TH1D*)fmpv->Get("hfit");
    if(!hfit){
      printf("no hfit!\n"); fmpv->ls(); exit(1);
    }
    else{
      printf("\n\n ************* hfit found***********************\n\n\n");
    }
  }

  //============================

  TChain * ch= XGLUtils::InputFiles(fin,"tree");

  ch->SetBranchStatus("*",0);
  
  ch->SetBranchStatus("p0",1);
  ch->SetBranchStatus("eta",1);
  ch->SetBranchStatus("pTPC",1);
  ch->SetBranchStatus("tpcsig",1);
  ch->SetBranchStatus("tpcncls",1);
  ch->SetBranchStatus("pTRD",1);
  ch->SetBranchStatus("trdsig",1);
  ch->SetBranchStatus("trdnch",1);
  ch->SetBranchStatus("trdncls",1);
  ch->SetBranchStatus("pidV0",1);
  //electron
  //[0] AliPID::kElectron
  //(const enum AliPID::EParticleType)0

  ch->SetBranchStatus("pidTPC0",1);

  Double_t p0, eta, pTPC, tpcsig, pTRD, trdsig, pidTPC0;
  Int_t tpcncls, trdnch, trdncls, pidV0;
  ch->SetBranchAddress("p0", &p0);
  ch->SetBranchAddress("eta", &eta);
  ch->SetBranchAddress("pTPC", &pTPC);
  ch->SetBranchAddress("tpcsig", &tpcsig);
  ch->SetBranchAddress("tpcncls", &tpcncls);
  ch->SetBranchAddress("pTRD", &pTRD);
  ch->SetBranchAddress("trdsig", &trdsig);
  ch->SetBranchAddress("trdnch", &trdnch);
  ch->SetBranchAddress("trdncls", &trdncls);
  ch->SetBranchAddress("pidV0", &pidV0);
  ch->SetBranchAddress("pidTPC0", &pidTPC0);

  fin=fin(fin.Last('/')+1, fin.Length());
  fin.Prepend("conv_");
  
  TTreeSRedirector *fStream = new TTreeSRedirector(fin);

  Long64_t nent = 0;
  while(ch->GetEntry(nent)){
    if(nent%1000000==0){
      printf("original fill %lld\n", nent);
    }
    nent++;

    Double_t mass = -999;
    if(pidV0==1)
      mass = Melectron();
    else if(pidV0==2)
      mass = Mpion();
    else if(pidV0==4)
      mass = Mproton();
    else
      continue;

    Double_t bg = pTRD/mass;

    Double_t expmpv = -999;
    if(hfit){
      expmpv = hfit->GetBinContent(hfit->FindBin(bg));
    }

    /*
    const Double_t cutbg = 2;

    if(pidV0==2 && bg<cutbg+0.1)
      continue;
    if(pidV0==4 && bg>cutbg-0.1)
      continue;
    */

    //printf("p0 %e pidV0 %d\n", p0, pidV0);

    (*fStream)<<"tree"<<
      "p0="<<p0<<
      "eta="<<eta<<

      "pTPC="<<pTPC<<
      "tpcsig="<<tpcsig<<
      "tpcncls="<<tpcncls<<

      "pTRD="<<pTRD<<
      "trdsig="<<trdsig<<
      "trdnch="<<trdnch<<
      "trdncls="<<trdncls<<

      "pidV0="<<pidV0<<
      "pidTPC0="<<pidTPC0<<

      "bg="<<bg<<

      "expmpv="<<expmpv<<

      "\n";

   
  }

  delete fStream;
}

int main(int argc, char * argv[])
{
  for(Int_t ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }

  if(argc!=2){
    printf("argc!=2\n"); return 1;
  }

  convertBG(argv[1]);

  printf("done!\n");
  return 0;
}
